//index.js
//获取应用实例
const app = getApp()
var utils = require('../../utils/util.js');
Page({
  data: {
    swiperimg: ['/images/index/7.png'],
    goods:[
      { img: '../../images/index/5.png', name: 'ELEFT足弓支撑舒适鞋垫', money: '23', stock:'20'},
      { img: '../../images/index/5.png', name: 'ELEFT足弓支撑舒适鞋垫', money: '23', stock: '20' },
      { img: '../../images/index/5.png', name: 'ELEFT足弓支撑舒适鞋垫', money: '23', stock: '20' }
    ]
  },
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    var peopleId = options.peopleId;
    wx.showLoading({
      title: '加载中',
    })
    that.setData({
      imgPath: app.d.imgPath
    })
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          //url: app.globalData.api_url + 'Homepage/openid',
          url: app.d.path + 'Index/openid',
          data: {
            code: res.code,
          },
          success: function (res) {
            console.log(res.data);
            var session_key = res.data.session_key;
            app.d.session_key = res.data.session_key;
            app.d.openid = res.data.openid;
            //that.init();
            wx.getSetting({
              success: res => {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                var code = res.code;
                if (res.authSetting['scope.userInfo']) {
                  wx.getUserInfo({
                    success: function (res) {
                      console.log(res.userInfo);
                      wx.request({
                        url: app.d.path + 'Index/user',
                        data: {
                          openid: app.d.openid,
                          img: res.userInfo.avatarUrl,
                          name: res.userInfo.nickName,
                          type: type,
                          people_id: peopleId
                        },
                        success: function (res) {
                          //console.log(res);
                         

                        },
                        fail: function (e) {
                          wx.showToast({
                            title: '获取用户信息失败！',
                            duration: 2000
                          });
                        },
                      });
                    }
                  });
                } else {
                  wx.reLaunch({
                    url: '/pages/login/login?code=' + code + '&type=' + type +'&peopleId=' + peopleId,
                  })
                }
              }
            })
          }
        })
      },
      fail: function () {
        console.log("登录失败")
      }
    });
    var sucFunc = function (that, res) {
      that.setData({
        swiperimg: res.data.banner,
        goods:res.data.goods
      })
      
      wx.hideLoading();
    }
    var data = { openid: app.d.openid }
    utils.wxRequest(that, app.d.path, "Index/index", data, sucFunc);
  },
  goods:function(){
    wx.navigateTo({
      url: "/pages/goodsclass/goodsclass"
    })
  },
  myArchives: function (e) {
    wx.navigateTo({
      url: "/pages/myArchives/myArchives"
    })
  },
  im: function (e) {
    wx.navigateTo({
      url: "/pages/im/im"
    })
  },
  foot: function () {
    wx.navigateTo({
      url: "/pages/footAnalysis/footAnalysis"
    })
  },
  detail: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/goodsDetail/goodsDetail?id=" + id
    })
  },
})
